package cn.edu.fzu.delivery.controller.sys;

import cn.edu.fzu.delivery.common.PageVO;
import cn.edu.fzu.delivery.common.Result;
import cn.edu.fzu.delivery.common.RetCode;
import cn.edu.fzu.delivery.domain.entity.Network;
import cn.edu.fzu.delivery.domain.entity.NetworkApply;
import cn.edu.fzu.delivery.domain.entity.SysUser;
import cn.edu.fzu.delivery.domain.enums.NetWorkStatusEnum;
import cn.edu.fzu.delivery.domain.enums.NetworkApplyStatusEnum;
import cn.edu.fzu.delivery.service.NetworkService;
import cn.edu.fzu.delivery.session.SessionConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author yifan
 * @date 2022/11/6
 */
@EnableOpenApi
@RestController
@RequestMapping("/api")
@Api(tags = "网点管理")
public class NetworkController {

    @Resource
    NetworkService networkService;

    @GetMapping("/auth/network/list")
    @ApiOperation(value = "获取网点列表（无无权限验证）", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "网点名称"),
            @ApiImplicitParam(name = "provinceId", value = "省id"),
            @ApiImplicitParam(name = "cityId", value = "市id" ),
            @ApiImplicitParam(name = "areaId", value = "地区id"),
            @ApiImplicitParam(name = "address", value = "网点地址"),
            @ApiImplicitParam(name = "status", value = "-1-倒闭,0-营业,1-不营业"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true)})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result list(String name, Long provinceId, Long cityId, Long areaId, String address,
                       NetWorkStatusEnum status, Integer pageNum, Integer pageSize) {
        try (Page<Network> page = PageHelper.startPage(pageNum, pageSize)) {
            networkService.listNetwork(name, provinceId, cityId, areaId, address, status);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }

    @GetMapping("/sys/network/search")
    @ApiOperation(value = "根据名称查询网点（无需登录）", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "网点名称",required = true)
    })
    public Result search(String name) {
        List<Network> networks = networkService.searchByName(name);
        return new Result(networks);
    }

    @PostMapping("/auth/sys/network/update")
    @ApiOperation(value = "系统管理员-更新网点信息", httpMethod = "POST", notes = "超级管理员可以更新任意的网点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true),
            @ApiImplicitParam(name = "name",value = "网点名称"),
            @ApiImplicitParam(name = "areaId", value = "地区id"),
            @ApiImplicitParam(name = "address", value = "网点地址"),
            @ApiImplicitParam(name = "desc", value = "网点描述")})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result update(Long id, String name, Long areaId, String address, String desc, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        networkService.update(id, name, areaId, address, desc, currentUser);
        return new Result(RetCode.SUCCESS, "修改成功");
    }

    @PostMapping("/auth/sys/network/change")
    @ApiOperation(value = "系统管理员-更新网点状态", httpMethod = "POST", notes = "超级管理员可以更新任意的网点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true),
            @ApiImplicitParam(name = "status", value = "-1-已倒闭，0-不营业，1-营业",required = true)})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result change(Long id, NetWorkStatusEnum status, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        networkService.updateStatus(id, status, currentUser);
        return new Result(RetCode.SUCCESS, "修改成功");
    }

    @PostMapping("/auth/sys/network/delete")
    @ApiOperation(value = "系统管理员-删除网点", httpMethod = "POST", notes = "只有超级管理员能删除网点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true)})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result delete(Long id, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        networkService.updateStatus(id, NetWorkStatusEnum.DELETE, currentUser);
        return new Result(RetCode.SUCCESS, "删除成功");
    }

    @PostMapping("/auth/sys/network/add")
    @ApiOperation(value = "系统管理员-添加网点信息", httpMethod = "POST", notes = "只有超级管理员能直接添加网点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "网点名称",required = true),
            @ApiImplicitParam(name = "areaId", value = "地区id",required = true),
            @ApiImplicitParam(name = "address", value = "网点地址",required = true),
            @ApiImplicitParam(name = "desc", value = "网点描述",required = true)})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result add(String name, Long areaId, String address, String desc, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        networkService.add(name, areaId, address, desc, currentUser);
        return new Result(RetCode.SUCCESS, "添加成功");
    }


    @Deprecated
    @PostMapping("/auth/sys/network/apply")
    @ApiOperation(value = "网点申请（暂不使用）", httpMethod = "POST", notes = "只有网点管理员能申请网点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "网点名称",required = true),
            @ApiImplicitParam(name = "areaId", value = "地区Id", required = true),
            @ApiImplicitParam(name = "address", value = "详细地址", required = true),
            @ApiImplicitParam(name = "desc", value = "备注") })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result apply(String name, Long areaId, String address, String desc, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        networkService.apply(name, areaId, address, desc, currentUser.getId(), currentUser);
        return new Result(RetCode.SUCCESS, "申请成功");
    }

    @Deprecated
    @PostMapping("/auth/sys/network/handleApply")
    @ApiOperation(value = "处理网点审核（暂不使用）", httpMethod = "POST", notes = "超级管理员审核网点\r\n审核通过则根据申请信息新增网点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "审核记录id", required = true),
            @ApiImplicitParam(name = "status", value = "设置审核结果:-1-不通过 1-通过", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result handleApply(Long id, NetworkApplyStatusEnum status, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        networkService.handleApply(id, status, currentUser);
        return new Result(RetCode.SUCCESS, "审核完成");
    }

    @Deprecated
    @GetMapping("/auth/sys/network/listApply")
    @ApiOperation(value = "获取网点申请列表（暂不使用）", httpMethod = "GET", notes = "超级管理员获取网点申请列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "desc", value = "备注"),
            @ApiImplicitParam(name = "applicantId", value = "申请人id"),
            @ApiImplicitParam(name = "reviewerId", value = "审核人id"),
            @ApiImplicitParam(name = "startTime", value = "申请时间不早于此时间"),
            @ApiImplicitParam(name = "endTime", value = "申请时间不晚于此时间"),
            @ApiImplicitParam(name = "status", value = "申请状态: -1-不通过 0-审核中 1-已通过"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result listApply(String name, Long provinceId, Long cityId, Long areaId, String address, String desc,
                            Long applicantId, Long reviewerId, NetworkApplyStatusEnum status, Date startTime, Date endTime,
                            Integer pageNum, Integer pageSize, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        try (Page<NetworkApply> page = PageHelper.startPage(pageNum, pageSize)){
            networkService.listApply(name, provinceId, cityId, areaId, address, desc, applicantId, reviewerId,
                    status, startTime, endTime, currentUser);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }
}
